package com.mkj.progress;

import javax.swing.*;
import java.awt.*;

@SuppressWarnings("all")
/**
 * @Author Arrebol
 * @Date 2024/8/6 20:53
 * @Project graphical programming
 * @Description：
 * 进度条组件基本演示
 */
public class JProgressBarTest3 {

    private final JFrame frame = new JFrame("测试进度条");
    JCheckBox uncertainProgress = new JCheckBox("不确定进度");
    JCheckBox noBorder = new JCheckBox("不绘制边框");
    //创建进度条
    JProgressBar progressBar  = new JProgressBar(JProgressBar.HORIZONTAL,0,100);
    //获取出进度条内置的数据模型对象
    BoundedRangeModel model = progressBar.getModel();
    public void init(){
        //组装视图
        //处理复选框的点击行为
        uncertainProgress.addActionListener(e -> {
            //获取复选框是否选中
            boolean isSelected = uncertainProgress.isSelected();
            //设置当前进度条不确定进度
            progressBar.setIndeterminate(isSelected);
            progressBar.setStringPainted(!isSelected);
        });
        noBorder.addActionListener(e -> {
            boolean isSelected = noBorder.isSelected();
            progressBar.setBorderPainted(!isSelected);
        });
        Box vBox = Box.createVerticalBox();
        vBox.add(uncertainProgress);
        vBox.add(noBorder);
        //设置进度条的属性
        progressBar.setStringPainted(true);
        progressBar.setBorderPainted(true);
        //将当前窗口布局设置为流式布局
        frame.setLayout(new FlowLayout());
        frame.add(vBox);
        frame.add(progressBar);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        //开启子线程模拟耗时操作
        SimulateActivity activity = new SimulateActivity(progressBar.getMaximum());
        new Thread(activity).start();
        //设置一个定时任务
        Timer timer = new Timer(200,e -> {
            //读取县城任务对象的当前完成量，设置给进度条
            int current = activity.getCurrent();
            model.setValue(current);
        });
        timer.start();
        //监听进度条的任务变化
        model.addChangeListener(e -> {
            int value = model.getValue();
            if(value == activity.getAmount()){
                timer.stop();
            }
        });
    }

    private class SimulateActivity implements Runnable{
        //记录任务总量
        private int amount;
        //记录当前任务的完成量
        private volatile int current;

        public int getAmount() {
            return amount;
        }

        public void setAmount(int amount) {
            this.amount = amount;
        }

        public int getCurrent() {
            return current;
        }

        public void setCurrent(int current) {
            this.current = current;
        }

        public SimulateActivity(int amount) {
            this.amount = amount;
        }

        public SimulateActivity() {
        }

        public SimulateActivity(int amount, int current) {
            this.amount = amount;
            this.current = current;
        }

        @Override
        public void run() {
            //子线程任务，模拟耗时操作
            while (current < amount){
                try {
                    Thread.currentThread().sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                current++;
            }
        }
    }
    public static void main(String[] args) {
        new JProgressBarTest3().init();
    }
}
